<template>
  <div class="my-container">
    <van-cell-group class="my-info" v-if="user">
      <van-cell class="base-info" center :border="false">
        <van-image class="avater" slot="icon" round fit="cover" :src="currentUser.photo" />
        <div slot="title" class="name">{{ currentUser.name }}</div>
        <van-button class="update-btn" size="small" round>编辑资料</van-button>
      </van-cell>
      <van-grid class="data-info" :border="false">
        <van-grid-item class="data-info-item">
          <div class="text-warp" slot="text">
            <div class="count">{{ currentUser.art_count }}</div>
            <div class="text">头条</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div class="text-warp" slot="text">
            <div class="count">{{ currentUser.follow_count }}</div>
            <div class="text">关注</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div class="text-warp" slot="text">
            <div class="count">{{ currentUser.fans_count }}</div>
            <div class="text">粉丝</div>
          </div>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <div class="text-warp" slot="text">
            <div class="count">{{ currentUser.like_count }}</div>
            <div class="text">获赞</div>
          </div>
        </van-grid-item>
      </van-grid>
    </van-cell-group>

    <div class="not-login" v-else>
      <div @click="$router.push('/login')"><img class="mobile" src="./下载.png" alt="" /></div>
      <div class="text">登录/注册</div>
    </div>

    <van-grid class="nav-grid mb-4" :column-num="2">
      <van-grid-item class="nav-grid-item1" icon="star-o" text="收藏" />
      <van-grid-item class="nav-grid-item2" icon="browsing-history-o" text="历史" />
    </van-grid>

    <van-cell title="线上通知" is-link to="/" />
    <van-cell class="mb-4" title="小智同学" is-link to="/" />
    <!-- 如果user有内容则显示退出登录按钮 -->
    <van-cell class="exit-btn" v-if="user" @click="onLogout" title="退出登录" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
// 将封装的请求当前用户信息的接口方法导入--- 并且在初始化created的时候来请求数据
import { getCurrentUser } from '@/api/userAPI.js'

export default {
  name: 'MyIndex',
  data() {
    return {
      currentUser: {} // 当前登录的用户信息
    }
  },
  created() {
    this.loadCurrentUser()
  },
  methods: {
    async loadCurrentUser() {
      const { data } = await getCurrentUser()
      console.log(data)
      this.currentUser = data.data
    },
    onLogout() {
      // 提示用户确认退出
      // 如果确认，处理退出
      this.$dialog
        .confirm({
          title: '退出提示',
          message: '是否确认退出'
        })
        .then(() => {
          // on confirm  确认退出
          // 清除登录状态
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    }
  },
  computed: {
    // 将容器中x的状态映射到本地
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.my-container {
  height: 100%;
  .my-info {
    background-image: url('./banner.png');
    background-size: cover;
    .base-info {
      padding-top: 38px;
      padding-bottom: 11px;
      box-sizing: border-box;
      height: 115px;
      background-color: unset;
      .avater {
        margin-right: 11px;
        width: 66px;
        height: 66px;
        border: 1px solid #fff;
      }
      .name {
        font-size: 15px;
        color: #fff;
      }
      .update-btn {
        height: 16px;
        font-size: 10px;
        color: #666;
      }
    }
    .data-info {
      .data-info-item {
        height: 65px;
        color: #fff;
        .text-warp {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .count {
            font-size: 18px;
          }
          .text {
            font-size: 11px;
          }
        }
      }
    }
    /deep/.van-grid-item__content {
      background-color: unset;
    }
  }
  .not-login {
    height: 180px;
    background-image: url('./banner.png');
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile {
      width: 66px;
      height: 66px;
    }
    .text {
      color: #fff;
      font-size: 14px;
    }
  }
  .nav-grid {
    font-size: 14px;
    .nav-grid-item1 {
      color: red;
    }
    .nav-grid-item2 {
      color: rgb(228, 142, 13);
    }
  }
  .van-cell {
    font-size: 16px;
  }
  .exit-btn {
    text-align: center;
    height: 50px;
    line-height: 30px;
    color: #d86262;
  }
  .mb-4 {
    margin-bottom: 6px;
  }
}
</style>
